Imports System.Data
Imports System.Data.SqlClient

Public Class CreateCreditCard

    Private Sub CreateCreditCard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadIcon(Me)
        LoadDataToLookUpEdit("SELECT * FROM Category_People ORDER BY CategoryName", "CategoryID", "CategoryName", "Category", cboCategory)
        Dim i As Integer
        For i = 2012 To 2100
            Me.cboYear.Properties.Items.Add(i)
        Next
    End Sub

    Private Sub chkNeverExpire_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkNeverExpire.CheckedChanged
        If Me.chkNeverExpire.Checked = True Then
            Me.cboYear.Enabled = False
            Me.cboMonth.Enabled = False
            Me.cboMonth.Text = "12"
            Me.cboYear.Text = "2100"
        Else
            Me.cboYear.Enabled = True
            Me.cboMonth.Enabled = True

        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()

    End Sub

    Private Function DataValidate() As Boolean

        If Me.chkNeverExpire.Checked = False Then
            If Me.cboYear.Text = "" Then
                MsgBox("Please Select Expire Date", MsgBoxStyle.Exclamation)
                cboYear.Focus()
                Return False
            End If

            If Me.cboMonth.Text = "" Then
                MsgBox("Please Select Expire Date", MsgBoxStyle.Exclamation)
                cboMonth.Focus()
                Return False
            End If
        End If

        Return True

    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If DxValidationProvider1.Validate = False Then
            Exit Sub
        End If

        If DataValidate() = False Then
            Exit Sub
        End If

        If MsgBox("Are You Sure Want to Create ?", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Exit Sub
        End If

        Dim str As String
        str = "INSERT INTO People (CodeNo, Name, Remark1, CreditBalance, ExpireDate, NeverExpireYesNo, Category) VALUES (@CodeNo, @Name, @Remark1, @CreditBalance, @ExpireDate, @NeverExpireYesNo, @Category)"
        Dim cmd As New SqlCommand(str, gCnnPCE)
        Dim ExpireDate As Date = New Date(Me.cboYear.Text, Me.cboMonth.Text, 1)
        Dim i As Integer
        For i = Me.txtFromCardNo.Text To Me.txtToCardNo.Text
            cmd.Parameters.Clear()
            cmd.Parameters.AddWithValue("@CodeNo", i)
            cmd.Parameters.AddWithValue("@Name", Me.txtName.Text)
            cmd.Parameters.AddWithValue("@Remark1", Me.txtRemark.Text)
            cmd.Parameters.AddWithValue("@CreditBalance", Me.txtCredit.Text)
            cmd.Parameters.AddWithValue("@ExpireDate", ExpireDate)
            cmd.Parameters.AddWithValue("@NeverExpireYesNo", chkNeverExpire.EditValue)
            cmd.Parameters.AddWithValue("@Category", Me.cboCategory.EditValue)
            cmd.ExecuteNonQuery()
        Next

        MsgBox("Successfully Create", MsgBoxStyle.Information)
        Me.Close()


    End Sub
End Class